package com.webwork.mood.common.controller;

import java.awt.image.BufferedImage;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Font;
import java.util.Random;
import java.io.IOException;
import javax.imageio.ImageIO;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletOutputStream;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class GenerateAuthCodeImageController {
	private String[] codex = new String[] {
			"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
			"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
			"K", "L", "M", "N", "P", "Q", "R", "S", "T", /* "O", */
			"U", "V", "W", "X", "Y", "Z" };

	private int codeLen = 4;
	private int width = 80;
	private int height = 20;

	@RequestMapping(value = "/verifyCode", method = RequestMethod.GET)
	@ResponseBody
	public void generateVerifyCode(HttpServletResponse response, HttpServletRequest request) throws Exception {
		Color backgroundColor = Color.WHITE;
		Color frontColor = Color.BLACK;
		String code = this.generateCharacter();
		Font font = new Font("Verdana", Font.BOLD, 18);
		Random random = new Random();
		BufferedImage image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = image.createGraphics();
		g.setFont(font);
		g.setColor(backgroundColor);
		g.fillRect(0, 0, this.width, this.height);
		g.setColor(frontColor);
		for (int i = 0; i < this.codeLen; i++) {
			String rand = code.substring(i, i + 1);
			g.setColor(new Color(20 + random.nextInt(60), 20 + random.nextInt(120), 20 + random.nextInt(180)));
			g.drawString(rand, 13 * (i + 1), 20);
		}

		for (int i = 0, n = random.nextInt(50); i < n; i++) {
			g.fillRect(random.nextInt(this.width), random.nextInt(this.height), 1, 1);
		}

		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");
		HttpSession session = request.getSession();
		session.setAttribute("verifyCode", code);
		try {
			ServletOutputStream so = response.getOutputStream();
			ImageIO.write(image, "JPEG", so);
			so.flush();
			so.close();
			response.flushBuffer();
		} catch (IOException ioe) {
			throw ioe;
		}
	}

	private String generateCharacter() {
		Random random = new Random();
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < this.codeLen; i++) {
			result.append(this.codex[random.nextInt(this.codex.length)]);
		}
		return result.toString();
	}
	
	@RequestMapping(value = "/getValidCode")
	@ResponseBody
	public String getValidCode(HttpServletRequest request) {
		return request.getSession(true).getAttribute("verifyCode").toString();
	}

}
